package me.luger.core.aspect.log;

import me.luger.core.commom.util.LogUtil;
import me.luger.core.mvc.WebService;
import me.luger.core.mvc.log.access.AccessLogEntity;
import me.luger.core.mvc.log.dao.DaoLogEntity;
import me.luger.core.mvc.log.service.ServiceLogEntity;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/21 0021.
 */
@Aspect
@Component
public class DaoLogAspect {

    ThreadLocal<Long> startTime = new ThreadLocal<Long>();
// || execution(* *com.jfinal.plugin.activerecord.Model.*(..))
    @Pointcut("execution(* *me.luger..dao*..*(..))")
    public void daoLog(){}

    @Around("daoLog()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        Object o = null;
        try{
            DaoLogEntity daoLogEntity = new DaoLogEntity();
            daoLogEntity.setBeginTime(System.nanoTime());
            AccessLogEntity accessLogEntity = WebService.getAccessLogEntity();
            daoLogEntity.setLogId(LogUtil.getLogId());
            o = pjp.proceed();
            daoLogEntity.setEndTime(System.nanoTime());
            List<DaoLogEntity> daoLogEntityList = accessLogEntity.getDaoLogEntityList();
            if(null == daoLogEntityList){
                daoLogEntityList = new ArrayList<DaoLogEntity>();
            }
            daoLogEntity.setExpend(daoLogEntity.getEndTime() - daoLogEntity.getBeginTime());
            daoLogEntityList.add(daoLogEntity);
            accessLogEntity.setDaoLogEntityList(daoLogEntityList);
            WebService.setAccessLogEntity(accessLogEntity);
        }catch (Exception e){
            o = pjp.proceed();
        }

        return o;
    }

}
